package com.litty.gftshp.server;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class AuthRemoteServiceServlet extends RemoteServiceServlet {

	public String processCall(String payload) throws SerializationException {
		 RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);
		 if (!rpcRequest.getMethod().getName().equals("login")){
		     if (!isSessionValid()){
		         return RPC.encodeResponseForFailure(null, new Exception());
		     }
		 }
		 return super.processCall(payload);
	}
	
	
	protected boolean isSessionValid() {
		HttpServletRequest httpReq = getThreadLocalRequest();
		HttpSession session = httpReq.getSession();
		
		String userId = (String) session.getAttribute("userid");
		if(userId != null) {
			return true;
		} else {
			return false;
		}
	}
}
